package com.example.administrator.xiaoxiang.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by Administrator on 2017/1/4.
 */

public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
    protected List<T> mdatas;

    protected Context mContext;
    protected int mLayoutResId;
    private OnItemClickListener mOnItemClickListener = null;


    public interface OnItemClickListener {
        void OnClick(View view, int position);
    }

    public BaseAdapter(Context context, List<T> datas, int LayoutResId) {
        this.mdatas = datas;
        this.mContext = context;
        this.mLayoutResId = LayoutResId;

    }


    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(mLayoutResId, parent, false);
        return new BaseViewHolder(view, mOnItemClickListener);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        T t = getItem(position);
        bindData(holder, t);
    }

    @Override
    public int getItemCount() {
        if (mdatas == null && mdatas.size() <= 0)

            return 0;
        return mdatas.size();
    }

    public T getItem(int postion) {
        return mdatas.get(postion);
    }

    public abstract void bindData(BaseViewHolder holder, T t);

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
